<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{
    public function index()
    {
        $user = Session::get('Muser');
        if($user){
            $this->redirect('/admin');
        }
        return $this->fetch('index/login');
    }

    public function login_do()
    {
        $userinfo=input('post.');
        //$captcha = input('verify');
        if(!captcha_check($userinfo['verify'])){
            //验证码错误
            $this->error('验证码错误','/admin/login');
        }else{
            //验证码正确
            $user=Db::name('admin')
                ->where('ad_name',$userinfo['adminname'])
                ->where('ad_pass',$userinfo['password'])
                ->find();
            if(empty($user)){
                $this->error('登录失败','/admin/login');
            }else{
                Session::set('Muser',$user['ad_name']);
                $this->success('登录成功', '/admin');
            }
        }

    }

    public function loginout()
    {
        Session::delete('Muser');
        $this->success('退出成功', '/admin/login');
    }
}
